<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片商品分类表格区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="addClassdsify">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <tree-table
        class="treeTable"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        border
      >
        <!-- 是否有效列demo -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 等级排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 0"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operation" slot-scope="scope">
          <el-button type="success" size="mini" @click="editClick(scope.row)"
            >编辑</el-button
          >
          <el-button type="warning" size="mini">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[
          queryInfo.pagesize,
          queryInfo.pagesize + 5,
          queryInfo.pagesize + 10,
          queryInfo.pagesize + 15,
        ]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框   -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <!-- 添加分类表单 -->

      <el-form
        :model="addCateForm"
        ref="addCateRef"
        label-width="100px"
        :rules="addCateFormRules"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            v-model="selectdKeys"
            :options="parentCateList"
            :props="{
              expandTrigger: 'hover',
              ...cascaderProps,
            }"
            @change="parentCateChanged"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import { goodsCateApi, getParentCateApi,addCateApi } from '../../request/api.js'
export default {
  data() {
    return {
      // 请求参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 3,
      },
      // 数据存放
      cateList: [],
      total: 0, //商品分类总条数
      //   绘制tree-table表格
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation',
        },
      ],
      addCateDialogVisible: false,
      parentList: [],//可选项数据源
      selectdKeys: [],//选中项绑定（绑定值id  key）
      // 添加分类表单数据对象
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0, //默认添加等级分类为一级
      },
      // 添加分类表单的验证规则对象
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      parentName: '', //父类名称
      parentCateList: [],
      // 指定级联选择器的配置对象
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
      },
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    getCateList() {
      goodsCateApi(this.queryInfo).then(({ data: res }) => {
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.cateList = res.data.result
        this.total = res.data.total
      })
    },
    //绑定编辑按钮
    editClick(id) {
      this.addCateDialogVisible = true
    },
    // 点击添加分类按钮
    addClassdsify() {
      this.addCateDialogVisible = true
      this.getParentCateList()
    },
    getParentCateList() {
      getParentCateApi().then(({ data: res }) => {
        console.log(res)
        if (res.meta.status !== 200) {
          return (this.$message.error = '获取父级分类失败')
        }
        this.parentList = res.data
        this.parentCateList = res.data
      })
    },
    // 监听pagesize事件，每页可以显示的条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    //改变当前的页数，最新的页码值，当前是第几页
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    // 选中项发生的变化
    parentCateChanged() {
      if( this.selectdKeys.length > 0 ){
         // 父级分类的id
        this.addCateForm.cat_pid = this.selectdKeys[this.selectdKeys.length - 1]
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = this.selectdKeys.length
        return 
      }else{
        // 父级分类的id
        this.addCateForm.cat_pid = 0
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = 0
      }
    },
    //点击添加分类中的确定提交按钮
    addCate() {
      console.log(this.addCateForm)
      addCateApi(this.addCateForm).then( ({data:res}) => {
        if( res.meta.status !== 201 ){
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialogVisible = false

      })
    },
    // 店家Dialog表单中的关闭按钮
    addCateDialogClosed() {
      
      this.$refs.addCateRef.resetFields()
      this.selectdKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    handleChange() {},
  },
}
</script>

<style lang="scss" scope>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>